package cn.edu.sctu.sctu_lysd.utils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import cn.edu.sctu.sctu_lysd.R;
import cn.edu.sctu.sctu_lysd.my_view.CustomDialog;


/**
 * Created by 鑫爷您慢点 on 2016/6/11.
 */
public class ProgressUtil {
    private Activity mActivity;
    private static ProgressUtil mProgressUtil = null;
    private View mView;
    private TextView mShowTextView;
    private ProgressDialog mProgressDialog;
    private ProgressBar mProgressBar;

    /**
     * 构造函数
     * @param activity
     */
    private ProgressUtil(Activity activity) {
        this.mActivity = activity;
    }

    /**
     * 单例模式
     * @param activity
     * @return
     */
    public static ProgressUtil getInstance(Activity activity){
        if (mProgressUtil == null){
            return new ProgressUtil(activity);
        }else {
         return mProgressUtil;
        }
    }

    /**
     * 获取自定义ProgressDialog
     * @param style 风格 圆环风格（ProgressDialog.STYLE_SPINNER） 水平风格（ProgressDialog.STYLE_HORIZONTAL）
     * @return
     */
    public ProgressDialog getProgressDialog(int style, boolean isOnTouch,String mShowText){
        /**
         * 获取自定义progressDialog布局文件
         */
        mView = LayoutInflater.from(mActivity).inflate(R.layout.view_progress,null,false);
        mShowTextView = (TextView) mView.findViewById(R.id.id_view_progress_show_text);

        mShowTextView.setText(mShowText);//设置显示文字

        mProgressDialog = new ProgressDialog(mActivity);
        mProgressDialog.setCancelable(isOnTouch);//是否可以被取消
//        mProgressDialog.setMessage(mShowText);//加载显示的信息

        mProgressDialog.setProgressStyle(style);//圆环风格ProgressDialog.STYLE_SPINNER
        mProgressDialog.show();
        //注意：setContentView()要在show()后使用
        mProgressDialog.setContentView(R.layout.view_progress);//自定义布局

        return mProgressDialog;
    }

    /**
     *
     */
    public void dismiss(ProgressDialog progressDialog){
        if (progressDialog != null && progressDialog.isShowing()){
            progressDialog.dismiss();
        }
    }

    public static void showAlertDialog(String title, String message, Context context, final AlertDialogCallBack callBack) {

        CustomDialog.Builder builder = new CustomDialog.Builder(context);
        builder.setMessage(message);
        builder.setTitle(title);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                callBack.submit();

            }
        });

        builder.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        callBack.cancel();
                    }
                });
        builder.create().show();
    }

  public interface AlertDialogCallBack{

        void submit();
        void cancel();

    }


}
